#!/bin/bash
###
 # @Author: dvlproad dvlproad@163.com
 # @Date: 2023-02-26 00:08:48
 # @LastEditors: dvlproad dvlproad@163.com
 # @LastEditTime: 2023-02-26 01:13:01
 # @FilePath: /AutoPackage-CommitInfo/bulidScriptCommon/brances_info/branch_detail_info_getter/test/tssh_branch_detail_info_getter.sh
 # @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
### 
# sh ./tssh_branch_detail_info_getter.sh


# 当前【shell脚本】的工作目录
# $PWD代表获取当前路径，当cd后，$PWD也会跟着更新到新的cd路径。这个和在终端操作是一样的道理的
CurrentDIR_Script_Absolute="$( cd "$( dirname "$0" )" && pwd )"
#echo "CurrentDIR_Script_Absolute=${CurrentDIR_Script_Absolute}"
#CommonFun_HomeDir_Absolute3=${CurrentDIR_Script_Absolute}/..
CommonFun_HomeDir_Absolute3=${CurrentDIR_Script_Absolute%/*} # 使用此方法可以避免路径上有..
CommonFun_HomeDir_Absolute2=${CommonFun_HomeDir_Absolute3%/*}
CommonFun_HomeDir_Absolute=${CommonFun_HomeDir_Absolute2%/*}

qscript_path_get_filepath="${CommonFun_HomeDir_Absolute}/qscript_path_get.sh"
qbase_update_json_file_singleString_script_path="$(sh ${qscript_path_get_filepath} qbase update_json_file_singleString)"


Develop_Branchs_FILE_PATH="${CurrentDIR_Script_Absolute}/data/tsdata_branches_detail_info.json"

Package_Environment_FILE_PATH="${CurrentDIR_Script_Absolute}/data/tsdata_env_info.json"

#echo "----------------------测试的数据buildContainBranchMaps=${buildContainBranchMaps}"


# echo "{}" > ${TEST_DATA_RESULT_FILE_PATH} #清空文件内容,但清空成{}

# 定义颜色常量
NC='\033[0m' # No Color
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'

# TODO:置空结果，还未验证通过
function emptyResultAndIfErrorExit() {
    sh "${qbase_update_json_file_singleString_script_path}" -jsonF "${Develop_Branchs_FILE_PATH}" -k "package_merger_branchs" -v "[]"
    if [ $? != 0 ]; then
        echo "❌Error: 执行 $FUNCNAME 时候出错啦。。。，请检查"
        exit 1
    fi
}

function test_getBranchsToFile() {
    echo "----------------------------------------------------------------------------test_getBranchsToFile"
    # emptyResultAndIfErrorExit
    function updateEnvKeyStringValue() {
        EnvKey=$1
        EnvValue=$2
        #echo "正在执行命令(更新路径到JSON文件中)：《sh \"${qbase_update_json_file_singleString_script_path}\" -jsonF \"${Package_Environment_FILE_PATH}\" -k \"${EnvKey}\" -v \"${EnvValue}\"》"
        sh "${qbase_update_json_file_singleString_script_path}" -jsonF "${Package_Environment_FILE_PATH}" -k "${EnvKey}" -v "${EnvValue}"
        if [ $? != 0 ]; then
            exit 1
        fi
    }
 
    App_Feature_Brances_Directory_PATH="${CurrentDIR_Script_Absolute}/featureBrances"

    # 获取当前分支所包含的分支及其分支信息
    REBASE_BRANCH="origin/develop"
    ignoreAddJsonFileNames=("dev_demo.json")
    ignoreCheckBranchNameArray=(master development dev_publish_out dev_publish_in dev_all)
    # verboseParamForSubitem="-verbose"
    # ignoreCheckBranchNameArray="ignoreAllCheck"
    verboseParamForSubitem="-null"
    echo "正在执行命令(获取当前分支所包含的分支及其分支信息)：《sh ${CommonFun_HomeDir_Absolute}/brances_info/all_branch_info_deal.sh -envInfoF \"${Package_Environment_FILE_PATH}\" -rebaseBranch \"${REBASE_BRANCH}\" -appBranchsDir \"${App_Feature_Brances_Directory_PATH}\" -branchInfoF \"${Develop_Branchs_FILE_PATH}\" -ignoreAddJsonFileNames \"${ignoreAddJsonFileNames}\" -ignoreCheckBranchs \"${ignoreCheckBranchNameArray[*]}\" -verboseSubitem \"${verboseParamForSubitem}\" 》"
    sh ${CommonFun_HomeDir_Absolute}/brances_info/all_branch_info_deal.sh -envInfoF "${Package_Environment_FILE_PATH}" -rebaseBranch "${REBASE_BRANCH}" -appBranchsDir "${App_Feature_Brances_Directory_PATH}" -branchInfoF "${Develop_Branchs_FILE_PATH}" -ignoreAddJsonFileNames "${ignoreAddJsonFileNames}" -ignoreCheckBranchs "${ignoreCheckBranchNameArray[*]}" -verboseSubitem "${verboseParamForSubitem}"
    if [ $? != 0 ]; then
        echo "❌Errror:测试 all_branch_info_deal 执行出错了。。。"
        exit 1
    fi
    echo "\n"
    echo "----------------------------------------处理后文件最后的完整内容 ${BLUE}${Develop_Branchs_FILE_PATH}${NC} ,如下:----------------------------"
    cat ${Develop_Branchs_FILE_PATH} | jq '.'
    
    # echo "{}" > ${TEST_DATA_RESULT_FILE_PATH} #清空文件内容,但清空成{}
    echo "\n\n"
}
test_getBranchsToFile